package com.zzyl.nursing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.domain.NursingProjectPlan;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.mapper.NursingProjectMapper;
import com.zzyl.nursing.mapper.NursingProjectPlanMapper;
import com.zzyl.nursing.service.INursingPlanService;

import com.zzyl.nursing.vo.NursingPlanVo;
import com.zzyl.nursing.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 护理计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-30
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;


    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id)
    {
        //查询护理计划
        NursingPlan byId = getById(id);
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        nursingPlanVo.setPlanName(byId.getPlanName());
        nursingPlanVo.setId(byId.getId());
        nursingPlanVo.setSortNo(Integer.valueOf(String.valueOf(byId.getSortNo())));
        nursingPlanVo.setStatus(Integer.valueOf(String.valueOf(byId.getStatus())));


        //查询护理计划对应的id信息
        LambdaQueryWrapper<NursingProjectPlan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(NursingProjectPlan::getPlanId,id);
        List<NursingProjectPlan> nursingProjectPlans = nursingProjectPlanMapper.selectList(queryWrapper);
        List<NursingProjectPlanVo> collect = nursingProjectPlans.stream().map(new Function<NursingProjectPlan, NursingProjectPlanVo>() {
            @Override
            public NursingProjectPlanVo apply(NursingProjectPlan nursingProjectPlan) {
                NursingProjectPlanVo nursingProjectPlanVo = new NursingProjectPlanVo();
                nursingProjectPlanVo.setExecuteCycle(String.valueOf(nursingProjectPlan.getExecuteCycle()));
                nursingProjectPlanVo.setExecuteFrequency(Long.valueOf(nursingProjectPlan.getExecuteFrequency()));
                nursingProjectPlanVo.setProjectId(String.valueOf(nursingProjectPlan.getProjectId()));
                BeanUtils.copyProperties(nursingProjectPlan, nursingProjectPlanVo);
                return nursingProjectPlanVo;
            }
        }).collect(Collectors.toList());

        nursingPlanVo.setProjectPlans(collect);

        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {

        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     * 
     * @param nursingPlan 护理计划
     * @return 结果
     */
    @Override
    public int insertNursingPlan(NursingPlan nursingPlan)
    {
        //nursingPlan.setCreateTime(DateUtils.getNowDate());
        return nursingPlanMapper.insertNursingPlan(nursingPlan);
    }

    /**
     * 修改护理计划
     * 
     * @param nursingPlan 护理计划
     * @return 结果
     */
    @Override
    public int updateNursingPlan(NursingPlanDto dto)
    {
        NursingPlan nursingPlan = new NursingPlan();
        nursingPlan.setSortNo(Long.valueOf(dto.getSortNo()));
        nursingPlan.setStatus(Long.valueOf(dto.getStatus()));
        nursingPlan.setId(Math.toIntExact(dto.getId()));
        BeanUtils.copyBeanProp(dto,nursingPlan);
        // 更新护理计划
        int count = nursingPlanMapper.updateById(nursingPlan);

        // 观察护理项目已计划表中的值
        if (dto.getProjectPlans() != null){
            LambdaQueryWrapper<NursingProjectPlan> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(NursingProjectPlan::getPlanId,dto.getId());
            nursingProjectPlanMapper.delete(queryWrapper);

            //之后进行插入
            List<NursingProjectPlan> projectPlans = dto.getProjectPlans();
            for (NursingProjectPlan projectPlan : projectPlans) {
                projectPlan.setPlanId(nursingPlan.getId());
                nursingProjectPlanMapper.insertNursingProjectPlan(projectPlan);
            }
        }
        return count == 0? 0 : 1;
    }

    /**
     * 批量删除护理计划
     * 
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids)
    {

        for (Long id : ids) {
            LambdaQueryWrapper<NursingProjectPlan> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(NursingProjectPlan::getPlanId,id);
            nursingProjectPlanMapper.delete(queryWrapper);
        }
        return nursingPlanMapper.deleteNursingPlanByIds(ids);
    }

    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id)
    {
        return nursingPlanMapper.deleteNursingPlanById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertNursingPlan(NursingPlanDto dto) {
        //新增护理计划
        //使用工具类实现
        NursingPlan nursingPlan = new NursingPlan();
        nursingPlan.setSortNo(Long.valueOf(dto.getSortNo()));
        nursingPlan.setStatus(Long.valueOf(dto.getStatus()));
        BeanUtils.copyBeanProp(nursingPlan,dto);

        int count =  this.nursingPlanMapper.insertNursingPlan(nursingPlan);

        //新增护理计划和护理项目的关联数据
        List<NursingProjectPlan> projectPlans = dto.getProjectPlans();
        for (NursingProjectPlan projectPlan : projectPlans) {
            projectPlan.setPlanId(nursingPlan.getId());
            nursingProjectPlanMapper.insertNursingProjectPlan(projectPlan);
        }

        return count == 0 ? 0 : 1;
    }
}
